<template>
  <div>
    <Row :gutter="10">
      <Col>
      <Card>
        <Row class="live_wrapper">
          <Button type="primary"
                  @click="addRole"
                  style="float:right;font-size: 14px;height: 34px;">添加角色</Button>
          <Input v-model="formValidate.name"
                 @on-search="search"
                 search
                 enter-button="搜索"
                 placeholder="请输入角色关键词"
                 style="float:right; width: 220px; margin-right: 10px;font-size: 14px;" />
        </Row>
        <Row class="margin-top-20 table_pattern">
          <Table :loading="loading"
                 :columns="columns"
                 :data="list"></Table>
        </Row>
        <Row class="margin-top-10">
          <Page :total="pagenation.totalPage"
                :page-size="pagenation.perPage"
                :current="pagenation.currentPage"
                show-elevator
                show-total
                @on-change="change"></Page>
        </Row>
      </Card>
      </Col>
    </Row>
    <Modal v-model="role_auth_show"
           title="权限详情"
           width="760"
           class="public_modal_header edit_modal">
      <div style="padding: 25px 0 35px 0;">
        <span v-for="(item,key) in role_auth"
              :key="key"
              :label="item.name"
              class="auth_span">{{item.name}} ({{item.description}})</span>
      </div>
    </Modal>
  </div>
</template>
<script>
import { roleList, deleteRole, getRoleAuth } from '@/api/main'
export default {
  data () {
    return {
      pagenation: {},
      columns: [
        {
          key: 'name',
          title: '角色名称',
          render: (h, params) => {
            return h('strong', params.row.name)
          },
        },
        {
          key: 'description',
          title: '角色描述',
          render: (h, params) => {
            return h('strong', params.row.description)
          },
        },
        {
          key: 'created_at',
          title: '创建时间',
          render: (h, params) => {
            return h('strong', params.row.created_at)
          },
        },
        {
          title: '操作',
          align: 'center',
          render: (h, params) => {
            return h('div', [
              h(
                'Button',
                {
                  props: {
                    size: 'small',
                  },
                  style: {
                    border: 0,
                    background: 'transparent',
                    fontSize: '14px',
                    color: '#377EEF',
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      this.showRoleAuth(params.row)
                    },
                  },
                },
                '查看权限',
              ),
              h(
                'Button',
                {
                  props: {
                    size: 'small',
                  },
                  style: {
                    border: 0,
                    background: 'transparent',
                    fontSize: '14px',
                    color: '#377EEF',
                    marginRight: '5px',
                  },
                  on: {
                    click: () => {
                      let routeData = routeData = this.$router.resolve({ name: 'edit_role', params: { name: params.row.name } })
                      window.open(routeData.href, '_blank')
                    },
                  },
                },
                '编辑',
              ),
              h(
                'Poptip',
                {
                  props: {
                    confirm: true,
                    title: '您确定要删除该角色吗?',
                    transfer: true,
                  },
                  on: {
                    'on-ok': () => {
                      deleteRole(params.row).then(res => {
                        if (res.code === 0) {
                          let _this = this
                          _this.$Message.success('删除成功')
                          _this.init()
                        } else {
                          this.$Message.error(res.text)
                        }
                      })
                    },
                  },
                },
                [
                  h(
                    'Button',
                    {
                      style: {
                        margin: '0 5px 0 0',
                        border: 0,
                        background: 'transparent',
                        fontSize: '14px',
                        color: '#377EEF'
                      },
                      props: {
                        placement: 'top',
                        size: 'small',
                      },
                    },
                    '删除',
                  ),
                ],
              ),
            ])
          },
        },
      ],
      list: [],
      role_auth: [],
      loading: true,
      role_auth_show: false,
      formValidate: {
        id: 0,
        name: '',
        description: '',
        page: 0,
      },
    }
  },
  components: {
  },
  methods: {
    change: function (page) {
      this.loading = true
      this.formValidate.page = page - 1
      this.init(page - 1)
    },
    addRole: function () {
      let routeData = this.$router.resolve({ name: 'add_role' })
      window.open(routeData.href, '_blank')
    },
    search: function () {
      this.formValidate.page = 0
      this.init()
    },
    showRoleAuth (obj) {
      this.role_auth_show = true
      let obj1 = {}
      obj1.name = obj.name
      getRoleAuth(obj1).then(res => {
        if (res.code === 0) {
          this.role_auth = res.data
        }
      })
    },
    init () {
      let obj = {}
      obj.page = this.formValidate.page
      obj.name = this.formValidate.name
      let _this = this
      roleList(obj).then(res => {
        if (res.code == 0) {
          _this.list = res.data
          _this.loading = false
          _this.pagenation.currentPage = parseInt(res.currentPage) + 1
          _this.pagenation.totalPage = parseInt(res.totalPage)
          _this.pagenation.perPage = parseInt(res.perPage)
        }
      })
    }
  },
  mounted () {
    this.init(0)
  },
}
</script>
<style>
.auth_span {
	width: 50%;
	display: inline-block;
	font-size: 16px;
	text-align: center;
	line-height: 40px;
}
</style>
